package com.primaryedu.ui;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.primaryedu.R;
import com.primaryedu.data.Config;
import com.primaryedu.data.HigtApplication;
import com.primaryedu.service.MessageService;
import com.primaryedu.utils.StatusBarCompat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 主界面
 *
 * @author jun.wang
 * @description
 * @data 2016年12月12日
 */
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements OnClickListener {
    private Fragment[] mFragments;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private RelativeLayout navi_item_home, navi_item_news;
    private RelativeLayout navi_item_course, navi_item_person;

    private ImageView image_home, image_news, image_course, image_person;
    private TextView text_home, text_news, text_course, text_person;

    private LocationClient mLocationClient;
    private LocationMode tempMode = LocationMode.Hight_Accuracy;
    private String tempcoor = "gcj02";
    private long exittime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        StatusBarCompat.compat(this, Color.BLACK);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();
        intent.setClass(MainActivity.this, MessageService.class);
        startService(intent);

        init();

    }

    private void init() {
        mFragments = new Fragment[4];
        fragmentManager = getSupportFragmentManager();
        mFragments[0] = fragmentManager.findFragmentById(R.id.fragement_home);
        mFragments[1] = fragmentManager.findFragmentById(R.id.fragement_news);
        mFragments[2] = fragmentManager
                .findFragmentById(R.id.fragement_course);
        mFragments[3] = fragmentManager.findFragmentById(R.id.fragement_personal);
        torawd(Config.towardIndex);

        findViewById(R.id.navi_item_home).setOnClickListener(this);
        findViewById(R.id.navi_item_news).setOnClickListener(this);
        findViewById(R.id.navi_item_course).setOnClickListener(this);
        findViewById(R.id.navi_item_person).setOnClickListener(this);

        image_home = (ImageView) this.findViewById(R.id.image_home);
        image_news = (ImageView) this.findViewById(R.id.image_news);
        image_course = (ImageView) this.findViewById(R.id.image_course);
        image_person = (ImageView) this.findViewById(R.id.image_person);
        text_home = (TextView) this.findViewById(R.id.text_home);
        text_news = (TextView) this.findViewById(R.id.text_news);
        text_course = (TextView) this.findViewById(R.id.text_course);
        text_person = (TextView) this.findViewById(R.id.text_person);
        setIndex(Config.towardIndex);
        mLocationClient = ((HigtApplication) getApplication()).mLocationClient;
//        initLocation();
    }

    private void torawd(int index) {
        fragmentTransaction = fragmentManager.beginTransaction()
                .hide(mFragments[0]).hide(mFragments[1])
                .hide(mFragments[2]).hide(mFragments[3]);

        if (index == 1) {
            fragmentTransaction.show(mFragments[0]).commit();
        } else if (index == 2) {
            fragmentTransaction.show(mFragments[1]).commit();
        } else if (index == 3) {
            fragmentTransaction.show(mFragments[2]).commit();
        } else if (index == 4) {
            fragmentTransaction.show(mFragments[3]).commit();
        }

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.navi_item_home:
                torawd(1);
                setIndex(1);
                break;

            case R.id.navi_item_news:
                torawd(2);
                setIndex(2);
                break;

            case R.id.navi_item_course:
                torawd(3);
                setIndex(3);
                break;

            case R.id.navi_item_person:
                torawd(4);
                setIndex(4);
                break;

            default:
                break;
        }
    }

    private void setIndex(int index) {
        // 请清除所有状态
        image_home.setImageResource(R.drawable.tab_home_default);
        image_news.setImageResource(R.drawable.tab_news_default);
        image_course.setImageResource(R.drawable.tab_course_default);
        image_person.setImageResource(R.drawable.tab_personal_default);
        text_home.setTextColor(getResources().getColor(R.color.text_default));
        text_news.setTextColor(getResources().getColor(R.color.text_default));
        text_course.setTextColor(getResources().getColor(R.color.text_default));
        text_person.setTextColor(getResources().getColor(R.color.text_default));

        switch (index) {
            case 1:
                image_home.setImageResource(R.drawable.tab_home_press);
                text_home.setTextColor(getResources().getColor(R.color.main_button));
                break;

            case 2:
                image_news.setImageResource(R.drawable.tab_news_press);
                text_news.setTextColor(getResources().getColor(R.color.main_button));
                break;

            case 3:
                image_course.setImageResource(R.drawable.tab_course_press);
                text_course.setTextColor(getResources().getColor(R.color.main_button));
                break;

            case 4:
                image_person.setImageResource(R.drawable.tab_personal_press);
                text_person.setTextColor(getResources().getColor(R.color.main_button));
                break;

            default:
                break;
        }
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(tempMode);//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType(tempcoor);//可选，默认gcj02，设置返回的定位结果坐标系，
        int span = 1000;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIgnoreKillProcess(true);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        mLocationClient.setLocOption(option);
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        mLocationClient.start();//定位SDK start之后会默认发起一次定位请求，开发者无须判断isstart并主动调用request
        mLocationClient.requestLocation();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exittime) > 2000) {
                Toast.makeText(this, getString(R.string.try_again), Toast.LENGTH_SHORT).show();
                exittime = System.currentTimeMillis();
            } else {
                // 同时清除缓存数据TODO
                System.exit(0);
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 当activity销毁时不保存其内部的view的状态
     *
     * @paramoutState
     */
    @SuppressLint("MissingSuperCall")
    @Override
    public void onSaveInstanceState(Bundle outState) {
//将super调用取消即可，表明当意外(比如系统内存吃紧将应用杀死)发生我不需要保存Fragmentde状态和数据等
//        super.onSaveInstanceState(outState);
    }
}
